package com.liuchuanzheng.lcz_study_demo.room

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.liuchuanzheng.lcz_study_demo.databinding.ActivityHookBinding
import com.liuchuanzheng.lcz_study_demo.MainActivity

import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.liuchuanzheng.lcz_study_demo.databinding.ActivityRoomBinding
import com.liuchuanzheng.lcz_study_demo.hook.TargetActivity
import com.liuchuanzheng.lcz_study_demo.util.log.LogUtil


class RoomActivity : AppCompatActivity() {
    lateinit var appDatabase :AppDatabase
    lateinit var userDao: UserDao
    lateinit var mViewBinding: ActivityRoomBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding = ActivityRoomBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
        appDatabase  = AppDatabase.getInstance(this)
        userDao = appDatabase.userDao()
        mViewBinding.btnAdd.setOnClickListener {
            for (i in 0 until 10){
                val user = User("刘传政${i}","河北${i}")
                userDao.addUser(user)
            }
        }
        mViewBinding.btnQueryAll.setOnClickListener {
            userDao.getUsers().forEach {
                LogUtil.i("==用户Id${it.id},姓名:${it.userName},地址:${it.address}")
            }
        }
        mViewBinding.btnQuery.setOnClickListener {
            var userById = userDao.getUserById(1)
            userById.let {
                LogUtil.i("==用户Id${it.id},姓名:${it.userName},地址:${it.address}")
            }

        }
        mViewBinding.btnDelete.setOnClickListener {
            var  user=User("","");
            user.id=1
            val deleteUserByUser = userDao.deleteByUser(user)
            if(deleteUserByUser!! >0){
                Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this,"id不存在",Toast.LENGTH_SHORT).show()

            }
        }
        mViewBinding.btnDeleteAll.setOnClickListener {
            userDao.deleteAll()
        }
        mViewBinding.btnUpdate.setOnClickListener {
            val user = User("张三", "修改地址")
            user.id = 21
            val updateUserByUser = userDao.updateUserByUser(user)
            Log.e("ss", updateUserByUser.toString())
            LogUtil.i(updateUserByUser.toString())

        }

    }

}